<?php

namespace model;

use common\Model;

class article_cate extends Model
{

	const TABLE = 'article_cate';
	protected $DB, $DBDD;
	function __construct($table = self::TABLE)
	{
		$this->DB = parent::db($table);
		$this->DBDD = parent::db();
	}

	public function FindData()
	{
		return $this->DB->where(['id' => $_GET['id']])->find();
	}

	//根据栏目权限获取栏目列表  设置$auth = 1  走权限，否则查询全部数据
	static public function SelectData($auth = null)
	{
		$pdo = \libs\pdo::Init();
		$prefix = $pdo->GetConfig();
		$table = $prefix['prefix'] . self::TABLE;
		if ($auth == 1 && LoginUserInfo('gid') != 1) {
			$channlid = implode(",", JsonDecode(LoginUserInfo("channlrole")));
			$sql = "SELECT * FROM `{$table}` WHERE `id` IN ({$channlid}) ORDER BY sort asc";
		} else {
			$sql = "SELECT * FROM `{$table}` ORDER BY sort asc";
		}
		return $pdo->QueryAll($sql);
	}

	//根据栏目权限获取栏目树列表
	public function SelectTreeData()
	{
		$cate = new \common\Category(array('id', 'pid', 'name', 'cname'));
		return $cate->GetTree(self::SelectData(1));
	}

	//添加数据
	public function insertData()
	{
		if (empty($_POST['name']))
			return ['status' => 0, 'msg' => '请填写必要数据'];

		$data = [
			'pid' => $_POST['pid'],
			'name' => VarFilter($_POST['name']),
			'type' => $_POST['type'],
			'theme' => VarFilter($_POST['theme']) ?? null,
			'contheme' => VarFilter($_POST['contheme']) ?? null,
			'sort' => $_POST['sort'] ?: 1,
			'num' => $_POST['num'] ?: 25,
			'color' => VarFilter($_POST['color']),
			'ct' => $_POST['ct'],
			'ico' => $_POST['ico'],
			'urlname' => VarFilter($_POST['urlname']) ?? null,
		];
		$re_key = $this->DB->insert($data);
		if ($re_key) {
			SqlLog("添加栏目 ID:" . $re_key);
			CleanDbCache(self::TABLE);
			return ['status' => 1, 'msg' => '添加成功'];
		} else {
			return ['status' => 0, 'msg' => '数据错误！添加失败'];
		}
	}

	//更新数据
	public function updateData()
	{
		if (empty($_POST['name']))
			return ['status' => 0, 'msg' => '请填写必要数据'];

		if ($_POST['pid'] == $_POST['id'])
			return ['status' => 0, 'msg' => '栏目不能选择自己作为父级'];

		$data = [
			'pid' => $_POST['pid'],
			'name' => VarFilter($_POST['name']),
			'type' => $_POST['type'],
			'theme' => VarFilter($_POST['theme']) ?? null,
			'contheme' => VarFilter($_POST['contheme']) ?? null,
			'sort' => $_POST['sort'] ?: 1,
			'num' => $_POST['num'] ?: 25,
			'color' => VarFilter($_POST['color']),
			'ct' => $_POST['ct'],
			'ico' => $_POST['ico'],
			'urlname' => VarFilter($_POST['urlname']) ?? null,
		];
		if ($this->DB->Where(['id' => $_POST['id']])->Update($data)) {
			SqlLog("修改栏目 ID:" . $_POST['id']);
			CleanDbCache(self::TABLE);
			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化'];
		}
	}

	//更新排序数据
	public function updateSort()
	{
		if ($this->DB->Where(['id' => $_GET['id']])->Update(['sort' => $_POST['sort']])) {
			SqlLog("修改内容栏目排序 ID:" . $_GET['id']);
			CleanDbCache(self::TABLE);
			return  ['status' => 1, 'msg' => '修改成功'];
		} else {
			return  ['status' => 0, 'msg' => '未修改'];
		}
	}

	//删除一条数据
	public function deleteOneData()
	{
		if ($this->DBDD->table('article')->where(['cid' => $_POST['id']])->find()) {
			return false; //如该栏目下有内容，拒绝删除
		} else {
			SqlLog("删除栏目：" . $_POST['id']);
			return $this->DB->where(['id' => $_POST['id']])->Delete();
		}
	}
}
